package com.ayuan.blog.controller;

import com.ayuan.blog.bean.WeChatMessage;
import com.ayuan.blog.common.WeChatUtil;
import com.ayuan.blog.common.util.JsonUtils;
import com.ayuan.blog.common.util.XmlUtils;
import com.ayuan.blog.common.wechat.message.WeChatMessageClient;
import com.ayuan.blog.common.wechat.user.WeChatUserClient;
import com.ayuan.blog.common.wechat.user.bean.WeChatUserInfo;
import com.ayuan.blog.domain.WxMessage;
import com.ayuan.blog.domain.WxUser;
import com.ayuan.blog.service.WxMessageService;
import com.ayuan.blog.service.WxUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by xiaozhu on 16/5/22.
 */
@Controller
@Log4j2
public class WeiXinController {

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private WxMessageService wxMessageService;

    @RequestMapping(value = "wx", method = RequestMethod.GET)
    @ResponseBody
    public String weiXin(String signature, String timestamp, String nonce, String echostr,
                         HttpServletRequest request, @RequestBody String body) {
        log.info("signature : "  + signature);
        log.info("timestamp : "  + timestamp);
        log.info("nonce : "  + nonce);
        log.info("echostr : "  + echostr);
        log.info("body : " + body);
        return echostr;
    }

    @RequestMapping(value = "wx", method = RequestMethod.POST)
    @ResponseBody
    public String weiXinMessage(HttpServletRequest request, @RequestBody String body) {
        log.info("body : " + body);
        try {
            WeChatMessage weChatMessage = XmlUtils.xmlMapper.readValue(body, WeChatMessage.class);
            log.info(JsonUtils.objectMapper.writeValueAsString(weChatMessage));
            if (weChatMessage != null && "text".equals(weChatMessage.getMsgType())) {
                WxUser wxUser = wxUserService.getByOpenId(weChatMessage.getFromUserName());
                if (null == wxUser) {
                    WeChatUserInfo weChatUserInfo = WeChatUserClient.userInfo(WeChatUtil.getToken(), weChatMessage.getFromUserName());
                    wxUser = new WxUser();
                    BeanUtils.copyProperties(weChatUserInfo, wxUser);
                    wxUser.setUpdateTime(new Date());
                    wxUserService.save(wxUser);
                }

                WxMessage wxMessage = new WxMessage();
                wxMessage.setUserId(wxUser.getWxUserId());
                wxMessage.setOpenId(weChatMessage.getFromUserName());
                wxMessage.setMsgType(weChatMessage.getMsgType());
                wxMessage.setContent(weChatMessage.getContent());
                wxMessage.setCreateTime(new Date(weChatMessage.getCreateTime() * 1000));
                wxMessageService.save(wxMessage);

//                String text = wxUser.getNickname() + "\n你好!";
//                WeChatMessageClient.messageCustomSend(WeChatUtil.getToken(), text, weChatMessage.getFromUserName());
                wxMessageService.dealWithWeChatMessage(weChatMessage.getFromUserName(),
                        wxUser.getWxUserId(), weChatMessage.getContent());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }
}
